package com.xiaowen.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowen.ssyx.acl.mapper.PermissionMapper;
import com.xiaowen.ssyx.acl.service.PermissionService;
import com.xiaowen.ssyx.acl.service.RolePermissionService;
import com.xiaowen.ssyx.acl.utils.PermissionHelper;
import com.xiaowen.ssyx.model.acl.AdminRole;
import com.xiaowen.ssyx.model.acl.Permission;
import com.xiaowen.ssyx.model.acl.RolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
        implements PermissionService {

    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 查询所有菜单
     */
    @Override
    public List<Permission> queryAllPermission() {
        //1 查询所有菜单
        List<Permission> allPermissionList =
                baseMapper.selectList(null);

        //2 转换要求数据格式
        return PermissionHelper.buildPermission(allPermissionList);
    }

    //递归删除菜单
    @Override
    public void removeChildById(Long id) {
        //1 创建list集合idList，封装所有要删除菜单id
        List<Long> idList = new ArrayList<>();

        //根据当前菜单id，获取当前菜单下面所有子菜单，
        //如果子菜单下面还有子菜单，都要获取到
        //重点：递归找当前菜单子菜单
        this.getAllPermissionId(id, idList);

        //设置当前菜单id
        idList.add(id);

        //调用方法根据多个菜单id删除
        baseMapper.deleteBatchIds(idList);
    }


    // 重点：递归找当前菜单下面的所有子菜单
    // 第一个参数是当前菜单id
    // 第二个参数最终封装list集合，包含所有菜单id
    private void getAllPermissionId(Long id, List<Long> idList) {
        // 根据当前菜单id查询下面子菜单
        // select * from permission where pid=2
        List<Permission> childList = baseMapper.selectList(new LambdaQueryWrapper<Permission>().eq(Permission::getPid, id));
        //递归查询是否还有子菜单，有继续递归查询
        childList.stream().forEach(item -> {
            // 封装菜单id到idList集合里面
            idList.add(item.getId());
            //递归
            this.getAllPermissionId(item.getId(), idList);
        });
    }

    /**
     * 根据角色id查询菜单数据
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Permission> getRolePermission(Long roleId) {
        //查询所有菜单数据
        List<Permission> allPermissions = baseMapper.selectList(null);
        //查询角色已经拥有的菜单数据
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId,roleId);
        List<RolePermission> exitPermission = rolePermissionService.list(queryWrapper);
        List<Long> list = exitPermission.stream().map(item -> item.getPermissionId()).collect(Collectors.toList());

        //遍历所有角色列表，将已经有的权限再角色列表中select属性置为true
        for (Permission permission : allPermissions){
            if (list.contains(permission.getId())){
                permission.setSelect(true);
            }
        }
        List<Permission> bulidList = PermissionHelper.buildPermission(allPermissions);
        return bulidList;
    }

    @Override
    public void saveRolePermission(Long roleId, Long[] permissionIds) {
        //删除原来的权限数据
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId,roleId);
        rolePermissionService.remove(queryWrapper);
        List<RolePermission> permissionList = new ArrayList<>();
        for (Long permissionId : permissionIds){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            permissionList.add(rolePermission);
        }
        rolePermissionService.saveBatch(permissionList);

    }





}

